<template>
  <div class="revise">
    <van-nav-bar
      title="修改地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      right-text="删除地址"
      @click-right="onClickRight"
    />
   
    <div class="address">
      <div class="name total">
        <span>修改姓名</span 
        ><input v-model="address.name" type="text" placeholder="姓名" />
      </div>

      <div class="phone total">
        <span>修改手机号</span
        ><input v-model="address.phone" type="text" placeholder="手机号" />
      </div>
      <div class="ads total">
        <span>修改收货地址</span
        ><input v-model="address.ads" type="text" placeholder="地址" />
      </div>
    </div>
    <div class="tr">
      <div class="del" @click="del">删除</div>
      <div class="seal" @click="edit">确定</div>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant';
import { address_edit, address_del } from "../axios/api";
export default {
  data() {
    return {
      address: {
        name: "",
        phone: "",
        ads: "",
        id: "",
      },
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
      },
      onClickRight() {
        
      },
    edit() {
        let id = JSON.parse(localStorage.getItem("userinfo")).id
      address_edit({
        userid: id,
        address: this.address.ads,
        phone: this.address.phone,
        name: this.address.name,
        addressid: this.address.id,
      }).then((res) => {
        //   if (this.address.name || this.address.phone || this.address.ads == "") {
        //     Toast.fail("请修改您的个人信息！")
        //   } else {
            Toast.success("修改成功！")
            this.$router.push("/address");
            console.log(res);
      });
    },
    del() {
      address_del({
        userid: JSON.parse(localStorage.getItem("userinfo")).id,
        addressid: this.address.id,
      }).then((res) => {
        this.$router.go(-1)
        this.$store.addressList = res.data.list
        console.log(res);
      });
    },
  },
  mounted() {
    // 获取路由参数
    let item = JSON.parse(this.$route.params.item);
    console.log(item);
    // 用$set更新页面数据
    this.$set(this.address, "name", item.name);
    this.$set(this.address, "phone", item.phone);
    this.$set(this.address, "ads", item.address);
    this.$set(this.address, "id", item.id||item.addressid);
    console.log(this.address.id);
  },
};
</script>
<style scoped lang="scss">
.address {
  display: flex;
  flex-direction: column;
  span {
    font-size: 16px;
    font-weight: 500;
    width: 100px;
  }
  .total {
    display: flex;
    margin: 15px 30px;
    input {
      display: block;
      height: 30px;
      border: none;
      outline: none;
      padding: 0 20px;
    }
  }
}
.tr {
  display: flex;
  justify-content: space-around;
  text-align: center;
  font-size: 20px;
  font-weight: 600;
  margin: 30px;
  height: 50px;
  line-height: 50px;
  .del {
    flex: 1;
    background-color: deepskyblue;
    margin-right: 10px;
    border-radius: 10px;
  }
  .seal {
    flex: 1;
    background: deepskyblue;
    border-radius: 10px;

  }
}
</style>